package com.yz.jdd.base.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.yz.jdd.R;
import com.yz.jdd.photo.view.PhotoView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bin on 2017/6/1.
 * 大图缩放预览
 */

public class PhotoViewActivity extends BaseActivity {
    private ViewPager viewPager;
    private ArrayList<String> images;
    private int currentIndex;//当前显示图片
    private TextView back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoview);
        initView();
    }

    void initView() {
        back= (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //当前从哪一张开始显示
        currentIndex = getIntent().getIntExtra("currentIndex", 0);


        images = getIntent().getStringArrayListExtra("files");
        viewPager = (ViewPager) findViewById(R.id.photo_pager);
        viewPager.setPageMargin((int) (getResources().getDisplayMetrics().density * 15));
        final List<PhotoView> photoViews=new ArrayList<>();
        if(images!=null&&images.size()>0){
            for (String image : images) {
                PhotoView view = new PhotoView(PhotoViewActivity.this);
                photoViews.add(view);
            }
        }

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return photoViews.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {

            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view=photoViews.get(position);
                view.enable();
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);


                try{
                    GlideUrl glideUrl = new GlideUrl(images.get(position), new LazyHeaders.Builder()
                            .addHeader("user-agent", ";androidapp/cross-bmios")
                            .build());
                    Glide.with(PhotoViewActivity.this).load(glideUrl).into(view);

                    container.addView(view);

                }catch (Exception e){

                }
                return view;

            }
        });

        viewPager.setCurrentItem(currentIndex);
    }
}
